package com.example.demo.demos.web.controller;

import com.example.demo.demos.web.entity.Car;
import com.example.demo.demos.web.entity.Gonggao;
import com.example.demo.demos.web.mapper.CarMapper;
import com.example.demo.demos.web.mapper.GonggaoMapper;
import com.example.demo.demos.web.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("index")
public class GonggaoController {
    @Autowired
    private GonggaoMapper gonggaoMapper;
    @Autowired
    private CarService carService;
    @GetMapping("/gonggao")
    public Map<String,Object> carindex(@RequestParam String title, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        Map<String,Object> res=new HashMap<>();
        pageNum=(pageNum-1)*pageSize;
        System.out.println(title);
        System.out.println(pageNum);
        System.out.println(pageSize);
        List<Gonggao> data=gonggaoMapper.selects(title,pageNum,pageSize);
        Integer total=gonggaoMapper.totalcar(title);

        res.put("code",200);
        res.put("data",data);
        res.put("total",total);

        return  res;
    }
    @PostMapping("/gonggaosave")
    public  Map<String,Object> carsave(@RequestBody Gonggao gonggao){
        Map<String,Object> res=new HashMap<>();
        if(gonggao.getId() == null){
            Integer issacve=gonggaoMapper.addcar(gonggao);
            if(issacve==1){
                res.put("code",200);
                res.put("msg","新增成功");

            }else{
                res.put("code",500);
                res.put("msg","新增失败");

            }
        }else{
            Integer issacve=gonggaoMapper.updatecar(gonggao);
            if(issacve==1){
                res.put("code",200);
                res.put("msg","修改成功");

            }else{
                res.put("code",500);
                res.put("msg","修改失败");

            }
        }

        return res;


    }
    @GetMapping("/shanchugg")
    public Map<String,Object> shanchu(@RequestParam String id){
        Map<String,Object> res=new HashMap<>();
        Integer issc= gonggaoMapper.shanchupp(id);
        if(issc == 1){
            res.put("code",200);
            res.put("msg","删除成功");
        }
        return res;
    }
    @GetMapping("/gonggaoall")
    public Map<String,Object> carindex1(){
        Map<String,Object> res=new HashMap<>();


        List<Gonggao> data=gonggaoMapper.selects1();


        res.put("code",200);
        res.put("data",data);


        return  res;
    }

}
